<?php

namespace App\HttpController\Admin;

use App\Utils\Code;
use EasySwoole\Component\Context\ContextManager;
use EasySwoole\HttpAnnotation\AnnotationTag\Di;
use EasySwoole\HttpAnnotation\AnnotationTag\InjectParamsContext;
use EasySwoole\HttpAnnotation\AnnotationTag\Param;

class Auth extends Base
{

    /**
     * @Di(key="adminNode")
     */
    protected $adminNode;
    /**
     * @Di(key="adminMenu")
     */
    protected $adminMenu;
    /**
     * @Di(key="adminRole")
     */
    protected $adminRole;


    /**
     * 添加节点
     * @InjectParamsContext(key="data")
     * @Param(name="node_name", notEmpty="不能为空")
     * @Param(name="path", notEmpty="不能为空")
     */
    public function addNode()
    {
        $data = ContextManager::getInstance() -> get('data');
        if ($this -> adminNode -> find(['path' => $data['path'] ?? ''])) return $this -> renderError(Code::ADD_EXISTS_FAILED);
        if ($this -> adminNode -> add($data)) return $this -> renderSuccess('添加成功');
        return $this -> renderError(Code::ADD_FAILED);
    }

    /**
     * 修改节点
     * @InjectParamsContext(key="data")
     * @Param(name="id", notEmpty="不能为空")
     * @Param(name="node_name")
     * @Param(name="path", optional="")
     */
    public function updateNode()
    {
        $param = ContextManager::getInstance() -> get('data');
        if ($this -> adminNode -> find(['path' => $param['path'], 'id' => [$param['id'], '!=']])) return $this -> renderError(Code::UPDATE_EXISTS_FAILED);
        if ($this -> adminNode -> edit($param)) return $this -> renderSuccess('修改成功');
        return $this -> renderError(Code::UPDATE_FAILED);
    }

    /**
     * 节点列表
     */
    public function nodeList()
    {
        $param = $this -> request() -> getRequestParam();
        if ($result = $this -> adminNode -> list($param)) return $this -> renderSuccessData($result);
        return $this -> renderError(Code::FAILED_TO_FIND_DATA);
    }

    /**
     * 添加菜单
     * @InjectParamsContext(key="data")
     * @Param(name="menu_name", notEmpty="不能为空")
     * @Param(name="path", optional="")
     * @Param(name="pid", notEmpty="不能为空")
     * @Param(name="node_ids", optional="")
     * @Param(name="sort", between={0,99}, optional="")
     */
    public function addMenu()
    {
        $data = ContextManager::getInstance() -> get('data');
        // if ($this -> adminMenu -> find(['path' => $data['path'] ?? ''])) return $this -> renderError(Code::ADD_EXISTS_FAILED);
        if ($this -> adminMenu -> add($data)) return $this -> renderSuccess('添加成功');
        return $this -> renderError(Code::ADD_FAILED);
    }

    /**
     * 修改菜单
     * @InjectParamsContext(key="data")
     * @Param(name="id", notEmpty="不能为空")
     * @Param(name="menu_name")
     * @Param(name="path")
     * @Param(name="pid")
     * @Param(name="node_ids")
     * @Param(name="sort", between={0,99}, optional="")
     */
    public function updateMenu()
    {
        $data = ContextManager::getInstance() -> get('data');
        // if ($this -> adminMenu -> find(['path' => $data['path'] ?? ''])) return $this -> renderError(Code::ADD_EXISTS_FAILED);
        if ($this -> adminMenu -> edit($data)) return $this -> renderSuccess('修改成功');
        return $this -> renderError(Code::ADD_FAILED);
    }

    /**
     * 菜单列表
     */
    public function menuList()
    {
        $param = $this -> request() -> getRequestParam();
        if ($result = $this -> adminMenu -> list($param)) return $this -> renderSuccessData($result);
        return $this -> renderError(Code::FAILED_TO_FIND_DATA);
    }

    /**
     * 添加角色
     * @InjectParamsContext(key="data")
     * @Param(name="role_name", notEmpty="不能为空")
     * @Param(name="menu_ids", optional="")
     */
    public function addRole()
    {
        $data = ContextManager::getInstance() -> get('data');
        if ($this -> adminRole -> find(['role_name' => $data['role_name'] ?? ''])) return $this -> renderError(Code::ADD_EXISTS_FAILED);
        if ($this -> adminRole -> add($data)) return $this -> renderSuccess('添加成功');
        return $this -> renderError(Code::ADD_FAILED);
    }

    /**
     * 修改角色
     * @InjectParamsContext(key="data")
     * @Param(name="id", notEmpty="不能为空")
     * @Param(name="role_name")
     * @Param(name="menu_ids")
     */
    public function updateRole()
    {
        $param = ContextManager::getInstance() -> get('data');
        if ($this -> adminRole -> find(['role_name' => $param['role_name'] , 'id' => [$param['id'], '!=']])) return $this -> renderError(Code::UPDATE_EXISTS_FAILED);
        if ($this -> adminRole -> edit($param)) return $this -> renderSuccess('修改成功');
        return $this -> renderError(Code::UPDATE_FAILED);
    }

    /**
     * 角色列表
     */
    public function roleList()
    {
        $param = $this -> request() -> getRequestParam();
        if ($result = $this -> adminRole -> list($param)) return $this -> renderSuccessData($result);
        return $this -> renderError(Code::FAILED_TO_FIND_DATA);
    }

}